<template>
  <div>
    <div class="operate">
      <el-button type="primary" size="small" class="operate-item" :disabled="!parentCode" @click="saveResourceVisible=true">新增</el-button>
    </div>

    <div class="data" v-loading="loading">
      <el-table :data="data.records">
        <el-table-column prop="code" label="编码" width="150"> </el-table-column>
        <el-table-column prop="name" label="名称" width="150"> </el-table-column>
        <el-table-column prop="operatePermName" label="权限名称" width="150"> </el-table-column>
        <el-table-column prop="operateHttpMethodName" label="方法" width="80"> </el-table-column>
        <el-table-column prop="operateHttpUrl" label="URL" min-width="150" width="auto"> </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showUpdate(scope.row.code)">编辑</el-button>
            <el-popconfirm title="确定要删除吗？" @onConfirm="remove(scope.row.code)">
              <el-button style="margin-left: 10px;" type="danger" size="mini" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="$config.defaultPageSizeOpts"
        :page-size="size"
        layout="total, sizes, prev, pager, next"
        :total="data.total">
      </el-pagination>
    </div>

    <SaveOperateResource :visible="saveResourceVisible"
                         :parentCode="parentCode"
                         @on-close="saveResourceVisible=false"
                         @on-success="saveSuccess">
    </SaveOperateResource>
    <UpdateOperateResource :visible="updateResourceVisible"
                           :parentCode="parentCode"
                           :code="code"
                           @on-close="updateResourceVisible=false"
                           @on-success="updateSuccess">
    </UpdateOperateResource>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
import SaveOperateResource from '@/views/system/resource/operate/SaveOperateResource'
import UpdateOperateResource from '@/views/system/resource/operate/UpdateOperateResource'

export default {
  name: 'OperateResource',
  components: { UpdateOperateResource, SaveOperateResource },
  props: {
    parentCode: {
      type: String
    }
  },
  data() {
    return {
      data: {},
      params: {},
      form: {},
      current: 1,
      size: 10,
      saveResourceVisible: false,
      updateResourceVisible: false,
      code: '',
      loading: false
    }
  },
  methods: {
    ...mapActions(['GetResourcePage', 'RemoveResourceById']),
    search() {
      if (!this.parentCode) return
      this.params.current = this.current
      this.params.size = this.size
      this.params.parentCode = this.parentCode
      this.params.typeCode = 2
      this.loading = true
      this.GetResourcePage(this.params).then(res => {
        this.loading = false
        if (res) {
          this.data = res
        }
      }).catch(() => {
        this.loading = false
      })
    },
    reset() {
      this.params = {}
      this.current = 1
    },
    searchByCondition() {
      this.current = 1
      this.search()
    },
    handleSizeChange(val) {
      this.size = val
      this.search()
    },
    handleCurrentChange(val) {
      this.current = val
      this.search()
    },
    saveSuccess() {
      this.search()
      this.saveResourceVisible = false
    },
    showUpdate(code) {
      this.code = code
      this.updateResourceVisible = true
    },
    updateSuccess() {
      this.search()
      this.updateResourceVisible = false
    },
    remove(code) {
      this.RemoveResourceById(code).then(() => {
        this.search()
      })
    }
  },
  watch: {
    parentCode (newVal, oldVal) {
      if (newVal && newVal !== oldVal) {
        this.search()
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .search {
    .search-item {
      width: 200px;
    }
  }
  .operate {
    float: right;
    operate-item {
      padding: 0 10px;
    }
  }
</style>
